Ext.define('Intranet.VeCongTy.view.MiniView', {
    extend: 'Ext.panel.Panel',
    requires: ['Intranet.Desktop.model.UserSetting'],
    alias: 'widget.VeCongTy_MiniView',
    title: 'Ve Cong Ty',
    height: 300,
    tools: [{
            type: 'gear',
            tooltip: 'Admin',
            hidden: true,
            id: 'VeCongTy_MiniView_Config_Tool',
            handler: function(event, toolEl, owner, tool) {
                Ext.create('Ext.window.Window', {
                    title: 'Admin',
                    height: 500,
                    width: 800,
                    layout: 'fit',
                    items: {
                        xtype: 'VeCongTy_AdminView'
                    }
                }).show();
            }
        }, {
            type: 'minimize',
            tooltip: 'Minimize',
            handler: function(event, toolEl, owner, tool) {
                owner.up('panel').collapse();
            }
        }, {
            type: 'restore',
            tooltip: 'Restore',
            handler: function(event, toolEl, owner, tool) {
                owner.up('panel').expand();
            }
        }, {
            type: 'maximize',
            tooltip: 'Maximize',
            handler: function(event, toolEl, owner, tool) {
                Ext.create('Ext.window.Window', {
                    title: 'Ve Cong Ty',
                    height: 500,
                    width: 800,
                    layout: 'fit',
                    items: {
                        xtype: 'VeCongTy_MainView'
                    }
                }).show();
            }
        }],
    layout: {
        type: 'hbox',
        align: 'stretch',
        padding: 10
    },
    items: [{
            xtype: 'container',
            margins: '0 10 10 0',
            flex: 7,
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            items: [{
                    xtype: 'panel',
                    margins: '0 0 7 0',
                    flex: 7,
                    title: 'Lien ket noi bo'
                }, {
                    xtype: 'panel',
                    flex: 3,
                    margins: '0 0 15 0',
                    title: 'Lien ket ngoai'
                }]
        }, {
            xtype: 'container',
            flex: 3,
            margins: '0 0 10 0',
            layout: {
                type: 'vbox',
                align: 'stretch'
            },
            items: [{
                    xtype: 'panel',
                    flex: 3,
                    margins: '0 0 7 0'
                }, {
                    xtype: 'panel',
                    id: 'VeCongTy_MiniView_BtnListPanel',
                    flex: 7,
                    margins: '0 0 15 0',
                    layout: {
                        type: 'vbox',
                        align: 'stretchmax'
                    },
                    border: false,
                    defaults: {
                        flex: 1,
                        margins: '7 0 7 0',
                        maxHeight: '30'
                    },
                    items: [{
                            xtype: 'button',
                            text: 'So do'
                        }]
                }]
        }],
    listeners: {
        afterrender: function(thisCmp, opts) {

            var panels = thisCmp.query('panel');

            for (var i = 0; i < panels.length; i++)
            {
                if (panels[i].getHeader()) {
                    panels[i].getHeader().getEl().applyStyles({
                        backgroundImage: "none",
                        backgroundColor: "#fff"
                    });
                }
            }

            Intranet.Desktop.model.UserSetting.load(0, {
                failure: function(record, operation) {
                    //do something if the load failed
                    //record is null
                },
                success: function(record, operation) {
                    if (record.data.username === 'khiem')
                    {
                        Ext.getCmp('VeCongTy_MiniView_Config_Tool').show();
                    }
                },
                callback: function(record, operation, success) {
                    //do something whether the load succeeded or failed
                    //if operation is unsuccessful, record is null
                }
            });
        }

    },
    initComponent: function()
    {
        this.callParent(arguments);
        var listBtnPanel = Ext.getCmp('VeCongTy_MiniView_BtnListPanel');
        Ext.Ajax.request({
            url: '/src/public/ve-cong-ty/tab/index',
            headers: {
                'Accept': 'application/json',
                'charset': 'utf-8'
            },
            success: function(response, opts) {
                var data = Ext.decode(response.responseText);

                if (data.success === 'true')
                {
                    var tabs = data.tabs;

                    for (var i = 0; i < tabs.length; i++)
                    {
                        var btn = Ext.widget('button', {
                            text: (tabs[i].name.length > 8) ? (tabs[i].name.substring(0, 7) + '...') : tabs[i].name
                        });

                        listBtnPanel.add(btn);
                    }
                }
            }
        });

    }

});
